package vn.vietel.pms.web.action;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.faces.convert.Converter;
import javax.faces.event.ActionEvent;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Factory;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.datamodel.DataModel;
import org.jboss.seam.annotations.datamodel.DataModelSelection;
import org.jboss.seam.faces.FacesMessages;
import org.jboss.seam.log.Log;

import vn.vietel.pms.converter.TeamTypeConverter;
import vn.vietel.pms.entity.VBTSStation;
import vn.vietel.pms.entity.VTeam;
import vn.vietel.pms.service.BTSStationService;
import vn.vietel.pms.service.TeamService;

@Scope(ScopeType.SESSION)
@Name("btsStationAction")
public class BTSStationAction {

	@In(required = false)
	@Out(required = false)
	@DataModelSelection("btsStationItems")
	private VBTSStation vbtsstation;

	@Logger
	Log log;
	@In("#{btsStationService}")
	private BTSStationService btsStationService;

	@In("#{teamService}")
	private TeamService teamService;

	@In
	private FacesMessages facesMessages;

	@DataModel
	private List<VBTSStation> btsStationItems;

	private Map<String, VTeam> teamList;

	private List<VTeam> teams;

	private Long id;

	private boolean editCompleted;

	private boolean editting;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
		editCompleted = false;
		editting = false;
		if (id != null) {
			vbtsstation = btsStationService.find(id);
			editting = (vbtsstation != null);
		} else {
			vbtsstation = new VBTSStation();
			editting = false;
		}
	}

	public void createItem() {
		try {
			editCompleted = false;
			VTeam v = teamService.find(vbtsstation.getBtsBelongTo().getId());
			System.out.println(vbtsstation.getBtsBelongTo().getTeamName());
			vbtsstation.setBtsBelongTo(v);
			btsStationService.create(vbtsstation);
			getBtsStationItems();
			editCompleted = true;
			facesMessages.add("BTS #{vbtsstation.btsName} has been added");
		} catch (IllegalStateException e) {
			facesMessages.add("Opps!!! Could not insert....");
			facesMessages.add(e.getMessage());
			editCompleted = false;
		}
	}

	public void updateItem() {
		try {
			editCompleted = false;
			btsStationService.update(vbtsstation);
			getBtsStationItems();
			editCompleted = true;
			editting = false;

		} catch (IllegalStateException e) {
			facesMessages.add(e.getMessage());
		}
	}

	public void deleteItem(ActionEvent ae) {
		try {
			Long rId = vbtsstation.getId();
			VBTSStation rgp = btsStationService.find(rId);
			btsStationService.delete(rgp);
			getBtsStationItems();
		} catch (IllegalStateException e) {
			facesMessages.add(e.getMessage());
		}
	}

	@Factory()
	public List<VBTSStation> getBtsStationItems() {
		btsStationItems = btsStationService.getAll();
		if (btsStationItems == null)
			btsStationItems = new LinkedList<VBTSStation>();
		facesMessages.add("Total "+btsStationItems.size()+" has been loaded.");
		return btsStationItems;
	}

	public void setBtsStationItems(List<VBTSStation> items) {
		this.btsStationItems = items;
	}

	public boolean isEditCompleted() {
		return editCompleted;
	}

	public void setEditCompleted(boolean editcompleted) {
		this.editCompleted = editcompleted;
	}

	public boolean isEditting() {
		return editting;
	}

	public void setEditting(boolean editting) {
		this.editting = editting;
	}

	public boolean notEditing() {
		return !editting;
	}

	@Factory("teamList")
	public Map<String, VTeam> getTeamList() {

		return teamList;
	}

	public void setTeamList(Map<String, VTeam> teamList) {
		this.teamList = teamList;
	}

	public Converter getConverter() {
		return new TeamTypeConverter(teams);
	}

	public List<VTeam> getTeams() {
		teams = btsStationService.getAllTeams();
		teamList = new TreeMap<String, VTeam>();
		for (VTeam t : teams) {
			System.out.println(t.getTeamName());
			teamList.put(t.getTeamName(), t);
		}

		return teams;
	}

	public void setTeams(List<VTeam> teams) {
		this.teams = teams;
	}

}
